<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="personNumber">
        <el-input
          v-model="queryParams.personNumber"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['manager:person:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="personList" row-key="id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column label="名称" align="left" prop="name" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="设备编码" align="center" prop="deviceCode" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['manager:person:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['manager:person:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改人员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="新增类型">
          <el-radio-group v-model="form.isOrganization">
            <el-radio :label="0">新增人员</el-radio>
            <el-radio :label="1">新增组织</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上级组织" prop="pId">
          <treeselect v-model="form.pId" :options="personOptions" :normalizer="normalizer" placeholder="请选择上级组织" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone" v-if="form.isOrganization == 0">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <!-- <el-form-item label="职位" prop="position" v-if="form.isOrganization ==0">
          <el-input v-model="form.position" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="介绍" prop="introduce" v-if="form.isOrganization ==0">
          <el-input v-model="form.introduce" placeholder="请输入介绍" />
        </el-form-item>
        <el-form-item label="座机" prop="telephone" v-if="form.isOrganization ==0">
          <el-input v-model="form.telephone" placeholder="请输入座机" />
        </el-form-item>
        <el-form-item label="性别" prop="sex" v-if="form.isOrganization ==0">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in sexOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="照片" prop="picture" v-if="form.isOrganization ==0">
          <el-input v-model="form.picture" placeholder="请输入照片" />
        </el-form-item>
        <el-form-item label="身份证号" prop="personNumber" v-if="form.isOrganization ==0">
          <el-input v-model="form.personNumber" placeholder="请输入身份证号" />
        </el-form-item> -->
        <!--<el-form-item label="openid" prop="openid" v-if="form.isOrganization ==0">
          <el-input v-model="form.openid" placeholder="请输入openid" />
        </el-form-item>-->
        <el-form-item label="设备编码" prop="deviceCode" v-if="form.isOrganization == 0">
          <el-input v-model="form.deviceCode" placeholder="请输入设备编码" />
        </el-form-item>

        <el-form-item v-if="form.isOrganization == 1">
          <template v-for="(item, index) in form.personnelJurisdictionList">
            <div :key="index" style="display: flex; margin-bottom:10px;">
              <el-form label-width="100px" style="flex:1;">
                <el-form-item label="高速名称" prop="jurisdictionName">
                  <el-input v-model="item.expresswayName" placeholder="请输入高速名称" />
                </el-form-item>
                <el-form-item label="开始公里数" prop="jurisdictionName" style="margin:5px 0;">
                  <el-input v-model="item.startKil" placeholder="请输入开始公里数" />
                </el-form-item>
                <el-form-item label="结束公里数" prop="jurisdictionName">
                  <el-input v-model="item.endKil" placeholder="请输入结束公里数" />
                </el-form-item>
              </el-form>
              <el-button type="primary" style="height: 34px; margin:0 10px;" @click="increaseaddsub()" v-if="index == 0">增加</el-button>
              <el-button type="primary" style="height: 34px; margin:0 10px;" @click="deleteaddsub(index)" v-if="index > 0">删除</el-button>
            </div>
          </template>
        </el-form-item>
        <!-- <el-form-item label="关联视频id" prop="relatedVideoId" v-if="form.isOrganization ==0">
          <el-input v-model="form.relatedVideoId" placeholder="请输入关联视频id" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPerson, getPerson, delPerson, addPerson, updatePerson, exportPerson, deleteList } from '@/api/person/person';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';

export default {
  name: 'Person',
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 人员表格数据
      personList: [],
      // 人员树选项
      personOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 性别字典
      sexOptions: [],
      // 查询参数
      queryParams: {
        name: null,
        phone: null,
        personNumber: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getDicts('sys_user_sex').then(response => {
      this.sexOptions = response.data;
    });
  },
  methods: {
    /** 查询人员列表 */
    getList() {
      this.loading = true;
      listPerson(this.queryParams).then(response => {
        this.personList = this.handleTree(response.data, 'id', 'pId');
        this.loading = false;
      });
    },
    /** 转换人员数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      listPerson().then(response => {
        this.personOptions = [];
        const data = { id: 0, name: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, 'id', 'pId');
        this.personOptions.push(data);
      });
    },
    // 性别字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pId: null,
        name: null,
        phone: null,
        position: null,
        introduce: null,
        telephone: null,
        createTime: null,
        sex: null,
        picture: null,
        personNumber: null,
        deviceCode: null,
        relatedVideoId: null,
        isOrganization: 0,
        personnelJurisdictionList: [
          {
            expresswayName: '',
            startKil: '', //开始公里数
            endKil: '' //结束公里数
          }
        ]
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTreeselect();
      this.open = true;
      this.title = '添加人员';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.pId = row.id;
      }
      getPerson(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改人员';
        console.log(this.form);
        if (this.form.personnelJurisdictionList.length == 0) {
          this.form.personnelJurisdictionList = [
            {
              expresswayName: '',
              startKil: '', //开始公里数
              endKil: '' //结束公里数
            }
          ];
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePerson(this.form).then(response => {
              this.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addPerson(this.form).then(response => {
              this.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除人员编号为"' + row.id + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return delPerson(row.id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess('删除成功');
        });
    },
    increaseaddsub() {
      this.form.personnelJurisdictionList.push({
        speedName: '', //高速名称
        startKil: '', //开始公里数
        endKil: '' //结束公里数
      });
    },
    deleteaddsub(index) {
      if (this.form.personnelJurisdictionList[index].id) {
        deleteList(this.form.personnelJurisdictionList[index].id);
      }
      this.form.personnelJurisdictionList.splice(index, 1);
    }
  }
};
</script>
